//
//  ViewController.m
//  05-UIScrollView
//
//  Created by dllo on 16/7/20.
//  Copyright © 2016年 DM.C. All rights reserved.
//

#import "ViewController.h"
// 宏定义命名的两种方式
// 宏嵌套
// 宏进行系统编译时, 代码的替换(预编译指令)
#define kScreenBounds [UIScreen mainScreen].bounds
// 1.全大写方式
#define WIDTH kScreenBounds.size.width
#define HEIGHT kScreenBounds.size.height
// 2.k + 驼峰命名
#define kScreenWidth kScreenBounds.size.width
#define kScreenHeight kScreenBounds.size.height


@interface ViewController ()<UIScrollViewDelegate>

@end

@implementation ViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view, typically from a nib.
    [self createScrollView];
    
}
#pragma mark - 创建scrollView和属性
- (void)createScrollView {
    UIScrollView *scrollView = [[UIScrollView alloc] initWithFrame:kScreenBounds];
    [self.view addSubview:scrollView];
    scrollView.backgroundColor = [UIColor cyanColor];
    UIButton *button = [UIButton buttonWithType:UIButtonTypeSystem];
    button.frame = CGRectMake(10, 10, WIDTH - 20, 100);
    button.backgroundColor = [UIColor redColor];
    [scrollView addSubview:button];
    // 1.contenSize(默认是CGSizeZero), 它是设置可以滚动的范围
    scrollView.contentSize = CGSizeMake(WIDTH, HEIGHT * 10);
    // 2.contentOffset(默认是CGSizeZero),
    // 偏移量, 当前的滚动位置(也就是说偏移了多少)
    // 问题: 改变contentOffset什么变了?
    // 答案: 改变contentOffset, scrollView的bounds改变了
    NSLog(@"offset改变前: scrollView frame is: %@", NSStringFromCGRect(scrollView.frame));
    NSLog(@"offset改变前: scrollView bounds is: %@", NSStringFromCGRect(scrollView.bounds));
    scrollView.contentOffset = CGPointMake(0, HEIGHT * 0);
    NSLog(@"offset改变后: scrollView frame is: %@", NSStringFromCGRect(scrollView.frame));
    NSLog(@"offset改变后: scrollView bounds is: %@", NSStringFromCGRect(scrollView.bounds));
    // 3.contentInset
    // 添加滚动区域四周的滚动范围
    scrollView.contentInset = UIEdgeInsetsMake(10, 10, 10, 10);
    // 4.directionalLockEnabled(BOOL类型, 默认是NO)
    // 锁定垂直或水平滚动(45°角, 锁会失效)rrgtrreg
    scrollView.directionalLockEnabled = YES;
    // 5.bounces 回弹效果(YES有, NO没有)
    scrollView.bounces = YES;
    // 6.alwaysBounceVertical
    // 在没有垂直方向的滚动范围时, 保证垂直方向的回弹效果存在
    scrollView.alwaysBounceVertical = YES;
    // 7.alwaysBounceHorizontal(水平方向)
    // 在没有水平方向的滚动范围时, 保证水平方向的回弹效果存在
    scrollView.alwaysBounceHorizontal = YES;
    // 8.pagingEnabled(默认是NO), 设置滑动时按页滚动(YES时开启)
    scrollView.pagingEnabled = YES;
    // 9.scrollEnabled(默认是YES)(NO时关闭scrollView的滑动)
    scrollView.scrollEnabled = YES;
    // 10.showsHorizontalScrollIndicator(默认是YES)
    // 设置水平方向上的滑动指示器是否隐藏(YES不隐藏, NO隐藏)
    scrollView.showsHorizontalScrollIndicator = YES;
    // 11.showsVerticalScrollIndicator(默认是YES)
    // 设置垂直方向上的滑动指示器是否隐藏(YES不隐藏, NO隐藏)
    scrollView.showsVerticalScrollIndicator = YES;
    // 12.scrollIndicatorInsets, 设置滑动指示器的位置
    scrollView.scrollIndicatorInsets = UIEdgeInsetsMake(10, 10, 10, 10);
    // 13.indicatorStyle(设置滑动指示器的样式, 枚举值(黑色背景, 白色背景, 默认色背景))
    scrollView.indicatorStyle = UIScrollViewIndicatorStyleDefault;
    // 14.setContentOffset:animated: 通过改变坐标改变偏移量
    // scrollRectToVisible:animated: 通过改变块的位置和大小来改变位置
//    [scrollView setContentOffset:CGPointMake(100, 100) animated:YES];
//    [scrollView scrollRectToVisible:CGRectMake(10, 10, 1000, 100) animated:YES];
    // 15.
    // tracking 用户是否开始拖动
//    scrollView.tracking
    // dragging 用户是否正在拖动
//    scrollView.dragging
    // decelerating 是否正在减速中
//    scrollView.decelerating
    // 16.scrollsToTop(默认是YES), 设置滚动视图双击状态栏回到首页的手势是否存在
    scrollView.scrollsToTop = YES;
    // 17.代理
    scrollView.delegate = self;
    
    UIImageView *image = [[UIImageView alloc] init];
    UIToolbar *toolbar = [[UIToolbar alloc] initWithFrame:CGRectMake(10, 10, image.frame.size.width, image.frame.size.height)];
    toolbar.barStyle = UIBarStyleBlackTranslucent;
    [image addSubview:toolbar];
    // 18.键盘消失模式
    scrollView.keyboardDismissMode = UIScrollViewKeyboardDismissModeOnDrag;
    // 创建一个textField用来测试键盘消失模式
    UITextField *text = [[UITextField alloc] initWithFrame:CGRectMake(10, 300, 394, 50)];
    text.backgroundColor = [UIColor yellowColor];
    [scrollView addSubview:text];
    // 19.缩放系数
    // 如果想进行视图缩放, 必须实现ViewForZoomingInScrollView:
    scrollView.minimumZoomScale = 0.50;
    scrollView.maximumZoomScale = 2.00;
    
    
}
#pragma mark - 代理方法
// 与想要缩放的子视图绑定 ,scrollView的contentSize会跟随这个子视图变化
- (UIView *)viewForZoomingInScrollView:(UIScrollView *)scrollView {
    return [scrollView.subviews firstObject];
}

// 滚动视图一滚动就执行
- (void)scrollViewDidScroll:(UIScrollView *)scrollView {
    // tracking  : 用户正在触摸
    if (scrollView.tracking) {
        NSLog(@"拖动滚动中");
    } else {
        NSLog(@"自己滚动中");
    }
    // dragging: 用户开始滑动
    if (scrollView.dragging) {
        NSLog(@"用户开始滑动");
    }
    // decelerting 用户触摸结束
    if (scrollView.decelerating) {
        NSLog(@"用户触摸结束");
    }
}
// 将要开始拖拽
- (void)scrollViewWillBeginDragging:(UIScrollView *)scrollView {
    NSLog(@"开始拖拽");
}
// 已经结束拖拽
- (void)scrollViewDidEndDragging:(UIScrollView *)scrollView willDecelerate:(BOOL)decelerate {
    NSLog(@"结束拖拽");
}
// 将要开始减速
- (void)scrollViewWillBeginDecelerating:(UIScrollView *)scrollView {
    NSLog(@"开始减速");
}
// 已经结束减速
- (void)scrollViewDidEndDecelerating:(UIScrollView *)scrollView {
    NSLog(@"结束减速");
}






- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

@end
